package com.baichang.taoshunda.dao.impl;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.dao.RecommendCompanyDao;
import com.baichang.taoshunda.entity.RecommendCompany;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/1/29 0029.
 */
@Repository
public class RecommendCompanyImpl extends BaseDaoImpl<RecommendCompany> implements RecommendCompanyDao {

    /**
     * 查询最新创建的数据id
     *
     * @return
     */
    @Override
    public Map<String, Object> queryInfoId() {
        String sql = "SELECT * FROM {table} ORDER BY created_time DESC LIMIT 1";
        return queryForMap(sql);
    }

    /**
     * 当前商家是否是推荐商家
     *
     * @param busId
     * @return
     */
    @Override
    public Map<String, Object> queryRecommendBus(Integer busId) {
        String sql = "SELECT * FROM {table} WHERE company_id=? AND status=1 AND auditing_status = 2 AND DATE_FORMAT(end_time,'%Y-%m-%d')>= DATE_FORMAT(NOW(),'%Y-%m-%d')";
        return queryForMap(sql, busId);
    }

    /**
     * 修改状态
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public int updateStaus(Integer id, Integer status) {
        String sql = "UPDATE {table} SET auditing_status=? WHERE id=?";
        return update(sql, status, id);
    }

    /**
     * 查询我的推荐商家
     *
     * @param busId
     * @return
     */
    @Override
    public Map<String, Object> queryRecommendBusDetails(Integer busId) {
        String sql = "SELECT * FROM {table} WHERE company_id=? AND status=1 AND auditing_status IN (1,2,3,4,5) AND DATE_FORMAT(end_time,'%Y-%m-%d')>= DATE_FORMAT(NOW(),'%Y-%m-%d')";
        return queryForMap(sql, busId);
    }

    /**
     * 推荐商家审核
     *
     * @param nowPage
     * @param pageSize
     * @param businessName
     * @return
     */
    @Override
    public Page queryAuditingBusiness(int nowPage, int pageSize, String businessName) {
        String sql = "SELECT rc.*,tb.buss_name AS busName,ch.payment_time AS payTime,ch.payment_money AS payMoney FROM t_recommend_company rc INNER JOIN t_bussiness tb ON rc.company_id=tb.id INNER JOIN t_company_panyment_history ch ON ch.recommend_id=rc.id " +
                "WHERE rc.auditing_status=1";
        if (!businessName.equals("")) {
            sql += " AND tb.buss_name LIKE '%" + businessName + "%' ";
        }
        sql += " ORDER BY rc.created_time DESC";
        return pageForMapList(sql, nowPage, pageSize);
    }

    /**
     * 同意推荐商家审核
     *
     * @param id
     * @param picture
     * @param code
     * @param startTime
     * @param endTime   @return
     */
    @Override
    public int replaceBusinessPicture(Integer id, String picture, Integer code, String startTime, String endTime) {
        String sql = "UPDATE {table} SET picture=?,auditing_status=?,start_time=?,end_time=? WHERE id=?";
        return update(sql, picture, code, startTime, endTime, id);
    }

    /**
     * @param id
     * @param picture
     * @return
     */
    @Override
    public int updatePic(Integer id, String picture) {
        String sql = "UPDATE {table} SET picture= ? WHERE id=?";
        return update(sql, picture, id);
    }

    /**
     * @param id
     * @return
     */
    @Override
    public List<Map<String, Object>> queryCompanyId(Integer id) {
        String sql = "SELECT id,IFNULL(end_time,0) AS endTime FROM t_recommend_company WHERE company_id=? AND status=1 ";
        return queryForMapList(sql, id);
    }

    @Override
    public Map<String, Object> queryRecommendDetails(Integer busId) {
        String sql = "SELECT * FROM {table} WHERE id=? ";
        return queryForMap(sql, busId);
    }

    /**
     * @param busId
     * @return
     */
    @Override
    public List<Map<String, Object>> queryAllHistory(Integer busId) {
        //AND DATE_FORMAT(end_time,'%Y-%m-%d')<= DATE_FORMAT(NOW(),'%Y-%m-%d')
        String sql = "SELECT *,picture AS picPath FROM t_recommend_company WHERE company_id= ? AND DATE_FORMAT(end_time,'%Y-%m-%d')<= DATE_FORMAT(NOW(),'%Y-%m-%d') ";
        return queryForMapList(sql, busId);
    }

    /**
     * @param recommendId
     * @param status
     * @return
     */
    @Override
    public int updateRecommendStaus(Integer recommendId, int status) {
        String sql = "UPDATE {table} SET status=? WHERE id=?";
        return update(sql, status, recommendId);
    }

    /**
     * @param busId
     * @return
     */
    @Override
    public Map<String, Object> queryCompanyIdInfo(Integer busId) {
        String sql = "SELECT IFNULL(end_time,0) AS endTime FROM {table} WHERE company_id=? AND status=1 ";
        return queryForMap(sql, busId);
    }

    /**
     * @param busId
     * @return
     */
    @Override
    public Map<String, Object> getAuditRecommentdBusiness(Integer busId) {
        String sql = "SELECT COUNT(*) AS num FROM {table} WHERE company_id=? AND status=1 AND auditing_status IN (1,4)";
        return queryForMap(sql, busId);
    }
}
